<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue分页组件</title>
    <style>
        .page {
            font-weight: 900;
            height: 40px;
            text-align: center;
            color: #888;
            margin: 20px auto 0;
            background: #f2f2f2;
        }

        .pagelist {
            font-size: 0;
            background: #fff;
            height: 50px;
            line-height: 50px;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }

        .pagelist span {
            font-size: 14px;
        }

        .pagelist .jump {
            border: 1px solid #ccc;
            padding: 5px 8px;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 4px;
            cursor: pointer;
            margin-left: 5px;
        }

        .pagelist .bgprimary {
            cursor: default;
            color: #fff;
            background: #337ab7;
            border-color: #337ab7;
        }

        .jumpinp input {
            width: 55px;
            height: 26px;
            font-size: 13px;
            border: 1px solid #ccc;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 4px;
            text-align: center;
        }

        .ellipsis {
            padding: 0px 8px;
        }

        .jumppoint {
            margin-left: 30px;
        }

        .pagelist .gobtn {
        }

        .bgprimary {
            cursor: default;
            color: #fff;
            background: #337ab7;
            border-color: #337ab7;
        }
    </style>
</head>

<body>
<div id="vuePaging" class="page" v-show="show">
    <div class="pagelist">
        <span class="jump" v-show="current_page>1" @click="jumpPage(current_page-1)">上一页</span>
        <span v-show="current_page>5" class="jump" @click="jumpPage(1)">1</span>
        <span class="ellipsis" v-show="efont">...</span>
        <span class="jump" v-for="num in indexs" :class="{bgprimary:current_page==num}"
              @click="jumpPage(num)">{{num}}</span>
        <span class="ellipsis" v-show="current_page<pages-4">...</span>
        <span v-show="current_page<pages-4" class="jump" class="jump" @click="jumpPage(pages)">{{pages}}</span>
        <span class="jump" v-show="current_page<pages" @click="jumpPage(current_page+1)">下一页</span>
        <span class="jumppoint">跳转到：</span>
        <span class="jumpinp"><input type="text" v-model="changePage"></span>
        <span class="jump gobtn" @click="jumpPage(changePage)">GO</span>
    </div>
</div>


<script src="js/vue.js"></script>
<script>
    var newlist = new Vue({
        el: '#vuePaging',
        data: {
            current_page: 1, //当前页
            pages: 50, //总页数
            changePage: '',//跳转页
            nowIndex: 0
        },
        computed: {
            show: function () {
                return this.pages && this.pages != 1
            },
            efont: function () {
                if (this.pages <= 7) return false;
                return this.current_page > 5
            },
            indexs: function () {
                var left = 1,
                        right = this.pages,
                        ar = [];
                if (this.pages >= 7) {
                    if (this.current_page > 5 && this.current_page < this.pages - 4) {
                        left = Number(this.current_page) - 3;
                        right = Number(this.current_page) + 3;
                    } else {
                        if (this.current_page <= 5) {
                            left = 1;
                            right = 7;
                        } else {
                            right = this.pages;

                            left = this.pages - 6;
                        }
                    }
                }
                while (left <= right) {
                    ar.push(left);
                    left++;
                }
                return ar;
            },
        },
        methods: {
            jumpPage: function (id) {
                if (!id || id < 1 || id > this.pages)return false;
                this.current_page = id;
                this.changePage = '';
            },
        },

    })
</script>

</body>
</html>
